﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public class 用户交互测试
    {
        [CommandMethod("TestGetSelect")]
        public static void TestGetSelect()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //生成三个圆并添加到当前模型空间
            Circle c1 = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
            Circle c2 = new Circle(Point3d.Origin, Vector3d.ZAxis, 20);
            Circle c3 = new Circle(Point3d.Origin, Vector3d.ZAxis, 30);
            Env.AddEntityToModelSpace(c1, c2, c3);
            PromptSelectionResult psr = ed.GetSelection(); //提示用户选择对象
            if (psr.Status != PromptStatus.OK) return;
            SelectionSet sSt = psr.Value; //获取选择集
            //信息提示框，给出选择集包含实体个数的提示
            Application.ShowAlertDialog("选择集中实体的数量:" + sSt.Count.ToString());
        }
        [CommandMethod("MergeSelection")]
        public static void GetSelection()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //第一次选择
            PromptSelectionResult ss1 = ed.GetSelection();
            if (ss1.Status != PromptStatus.OK) return;
            Application.ShowAlertDialog("第一个选择集中实体的数量:" + ss1.Value.Count.ToString());
            //第二次选择
            PromptSelectionResult ss2 = ed.GetSelection();
            if (ss2.Status != PromptStatus.OK) return;
            Application.ShowAlertDialog("第二次选择集中实体的数量:" + ss2.Value.Count.ToString());
            //第二次选择集中的ObjectId加入到第一个选择集中
            var ss3 = ss1.Value.GetObjectIds().Union(ss2.Value.GetObjectIds());
            Application.ShowAlertDialog("合并后选择集中的数量:" + ss3.Count().ToString());
        }
        [CommandMethod("TestPickFirst", CommandFlags.UsePickSet)]
        public static void TestPickFirst()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //获取当前已选择的实体
            PromptSelectionResult psr = ed.SelectImplied();
            //在命令发出前已有实体被选中
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet ss1 = psr.Value;  //获取选择集
                //显示当前已经选则的实体个数
                Application.ShowAlertDialog("PickFirst示例:当前已选则的实体个数:" + ss1.Count.ToString());
                //清空当前选则集
                ed.SetImpliedSelection(new ObjectId[0]);
                psr = ed.GetSelection();  //提示用户进行新的选则
                if (psr.Status == PromptStatus.OK)
                {
                    //设置当前已选则的实体
                    ed.SetImpliedSelection(psr.Value.GetObjectIds());
                    SelectionSet ss2 = psr.Value;
                    Application.ShowAlertDialog("PickFirst示例:当前已选则的实体个数:" + ss2.Count.ToString());
                }
                else
                {
                    Application.ShowAlertDialog("PickFirst示例:当前已选则的实体个数:0");
                }
            }
        }
        //多边形作边界的选则
        [CommandMethod("TestPolygonSelect")]
        public static void TestPolygonSelect()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //声明一个Point3d类列类对象，用于存储多线段的顶点
            Point3dCollection pts = new Point3dCollection();
            //提示用户选则多线段
            PromptEntityResult per = ed.GetEntity("请选则多段线");
            if (per.Status != PromptStatus.OK) return;
            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                //转换Polyline对象
                Polyline pline = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline;
                if (pline != null)
                {
                    //遍历所选多段线的顶点并添加到Point3d集合中
                    for (int i = 0; i < pline.NumberOfVertices; i++)
                    {
                        pts.Add(pline.GetPoint3dAt(i));
                    }
                    //窗口选则，仅选则完全位于多边形区域中的对象
                    PromptSelectionResult psr = ed.SelectWindowPolygon(pts);
                    if (psr.Status == PromptStatus.OK)
                    {
                        Application.ShowAlertDialog("选则集中实体的数量:" + psr.Value.Count.ToString());
                    }
                }
                trans.Commit();
            }
        }
        //交叉选择模式
        [CommandMethod("TestSelectException")]
        public static void TestSelectException()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Point3d pt1 = Point3d.Origin;
            Point3d pt2 = new Point3d(100, 100, 0);
            //交叉窗口选则，选则由pt1和pt2组成的矩形窗口包围或相交的对象
            PromptSelectionResult psr = ed.SelectCrossingWindow(pt1, pt2);
            if (psr.Status == PromptStatus.OK)
            {
                Application.ShowAlertDialog("选则集中实体的数量L:" + psr.Value.Count.ToString());
            }
        }
        [CommandMethod("TestFilter")]
        public static void TestFilter()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //创建一个自定义的TypedValue列表对象，用于构建过滤器列表
            TypedValueList values = new TypedValueList();
            values.Add(DxfCode.LayerName, "图层1");
            values.Add(typeof(Line));
            //构建过滤器列表，注意这里使用自定义类型转换
            SelectionFilter filter = new SelectionFilter(values);
            //选则图形中所有满足过滤器的对象，即位于图层1上的直线
            PromptSelectionResult psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
            {
                Application.ShowAlertDialog("选择集中实体的数量:" + psr.Value.Count.ToString());
            }
        }
        [CommandMethod("FilterSelectionSet")]
        public static void FilterSelectionSet()
        {
            //获取当前文档编辑器
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            TypedValue[] values = new TypedValue[1];
            values.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0);
            //将过滤器条件赋值给SelectionFilter对象
            SelectionFilter filter = new SelectionFilter(values);
            //请求用户在图形区域选则对象
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSt = psr.Value;
                Application.ShowAlertDialog("Number reof objects selected:" + sSt.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected:0");
            }
        }
        [CommandMethod("FilterBlueCircleOnLayer0")]
        public static void FilterBlueCircleOnLayer0()
        {
            //获取当前文档编辑器
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //创建TypeValue数组定义过滤条件
            TypedValue[] values = new TypedValue[3];
            values.SetValue(new TypedValue((int)DxfCode.Color, 5), 0);
            values.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 1);
            values.SetValue(new TypedValue((int)DxfCode.LayerName, "0"), 2);
            //将过滤器条件赋值给SelectionFilter对象
            SelectionFilter filter = new SelectionFilter(values);
            //请求在图形区域选则对象
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSt = psr.Value;
                Application.ShowAlertDialog("Number of objects selected:" + sSt.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected:0");
            }
        }
        [CommandMethod("FilterRelational")]
        public static void FilterReational()
        {
            //获取当前文档编辑器
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //创建TypeValue数组，定义过滤条件
            TypedValue[] values = new TypedValue[3];
            values.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0);
            values.SetValue(new TypedValue((int)DxfCode.Operator, ">="), 1);
            values.SetValue(new TypedValue(40, 5), 2);
            //将过滤条件赋给SelectionFilter对象
            SelectionFilter filter = new SelectionFilter(values);
            //请求在图形区域选则对象
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSt = psr.Value;
                Application.ShowAlertDialog("Number of objects selected:" + sSt.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected:0");
            }
        }

        [CommandMethod("FilterForText")]
        public static void FilterForText()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //创建TypedValue数组，定义过滤条件
            TypedValue[] values = new TypedValue[4];
            values.SetValue(new TypedValue((int)DxfCode.Operator, "<or"), 0);
            values.SetValue(new TypedValue((int)DxfCode.Start, "TEXT"), 1);
            values.SetValue(new TypedValue((int)DxfCode.Start, "MTEXT"), 2);
            values.SetValue(new TypedValue((int)DxfCode.Operator, "or>"), 3);
            //将过滤条件赋给SelectionFilter对象
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSt = psr.Value;
                Application.ShowAlertDialog("Number of objects selected:" + sSt.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected:0");
            }
        }

        [CommandMethod("FilterMtextWildcard")]
        public static void FilterMtextWildcard()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //创建TypeValue数组，定义过滤条件
            TypedValue[] values = new TypedValue[2];
            values.SetValue(new TypedValue((int)DxfCode.Start, "MTEXT"), 0);
            values.SetValue(new TypedValue((int)DxfCode.Text, "*The*"), 1);
            //将过滤条件赋值给SelectionFilter对象
            SelectionFilter filter = new SelectionFilter(values);
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.OK)
            {
                SelectionSet sSt = psr.Value;
                Application.ShowAlertDialog("Number of objects selected:" + sSt.Count.ToString());
            }
            else
            {
                Application.ShowAlertDialog("Number of objects selected:0");
            }
        }
    }
}
